/**
 * Created by Administrator on 2017/6/8 0008.
 */

//import {runSqlInfo} from '../common/dataAccess';
var db = require('../common/dataAccess');

exports.getNewsInfo = function(parame,req,cb){
    console.log("获取新闻头部信息",parame);
    var obj = {};
    obj.commands = "select * from topinfo;";

    db.runSqlInfo(obj,function(info){
        console.log("info is:",info);
        cb({code:200,info:info});
    })
}



var updateNewsInfo = function(parame,req,cb){
    console.log("修改新闻头部信息");
}



exports.getNewsInfoDetail =  function(parame,req,cb){
    console.log("获取新闻头部信息",parame);
    var obj = {};
    if(!parame.id)
        return cb({code:500});

    obj.commands = "select * from topinfo where  topId = " + parame.id;;
    db.runSqlInfo(obj,function(info){
        console.log("info is:",info[0]);
        info[0].detailContent = info[0].detailContent.replace(/\n/g,"\r\n");
        console.log("获取新闻具体信息info",info);
        cb({code:200,info:info});
    })

}


exports.getAboutAsInfo =  function(parame,req,cb){

    console.log("获取关于我们的内容",parame);
    var obj = {};
    if(parame==0){
        obj.commands = "select * from aboutus;";
    }
    db.runSqlInfo(obj,function(info){
        console.log("info is:",info);
        cb({code:200,info:info});
    })
}


var updateAboutAsInfo = function(parame,req,cb){
    console.log("修改关于我们的内容");
}

//获取手机版本首页内容
exports.getindexContent =  function(parame,req,cb){
    var obj = {};
    if(parame==0){
        obj.commands = "select * from indexinfo;";
    }
    db.runSqlInfo(obj,function(info){
        console.log("info is:",info);
        cb({code:200,info:info});
    })
}


//获取网页版本首页内容
exports.getPcindexContent =  function(parame,req,cb){
    var obj = {};
    if(parame==0){
        obj.commands = "select * from pcindex;";
    }
    db.runSqlInfo(obj,function(info){
        console.log("info is:",info);
        cb({code:200,info:info});
    })
}




exports.getBossCompneyImag =  function(parame,req,cb){

    console.log("获取首页内容",parame);
    var obj = {};

        obj.commands = "select * from companyimg;";

    db.runSqlInfo(obj,function(info){
        console.log("info is:",info);
        cb({code:200,info:info});
    })
}

exports.getContactInfo =  function(parame,req,cb){

    console.log("联系方式",parame);
    var obj = {};

    obj.commands = "select * from contact;";

    db.runSqlInfo(obj,function(info){
        console.log("info is:",info);
        cb({code:200,info:info});
    })
}


exports.ChangeContactInfo =  function(parame,req,cb){

    console.log("修改联系方式",parame);
    var title = parame.title;
    var content = parame.content;
    var id = parame.id;

    var obj = {};
    obj.commands = "update  contact  set title = '" + title + "'"
    + " , content ='" + content + "'"
    + " where id = " + id;

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err){
            return cb({code:500,info:"修改数据库失败"})
        }else
            cb({code:200,info:'修改成功'});
    })
}


exports.changeNewsInfo =  function(parame,req,cb){

    console.log("修改新闻信息",parame);

    var title = parame.title;
    var time = parame.time;
    var topContent = parame.topContent;
    var detailContent = parame.detailContent;
    var topId = parame.topId;

    var obj = {};
    obj.commands = "replace into topinfo (topId,title,time,topContent,detailContent) values("
        +" '"+  topId  + " ',"
        +" '"+  title  + " ',"
        +" '"+  time  + " ',"
        +" '"+  topContent  + "' ,"
        +" '"+  detailContent  +"'"
        + ")";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err){
            return cb({code:500,info:"修改数据库失败"})
        }else
            cb({code:200,info:'修改成功'});
    })
}
const  fs = require('fs');
const  path = require('path');

exports.upload =  function(res,req,cb){
    var body = req.body;
    console.log("req.body",req.files);
    console.log("req.body",req);

    var filename = req.files.files.originalFilename;
    var url = body.url;
    var id = body.id;
    var Nmysql = body.Nmysql;

    var originalPath = req.files.files.path;
    var targetPath = path.join(__dirname, '../public'+url);
    console.log("targetPath is",targetPath)
    console.log("body is",body)
    console.log("path.dirname(__filename) is",__dirname)
    //path.dirname()
    fs.createReadStream(originalPath).pipe(fs.createWriteStream(targetPath));

    if(Nmysql != 1){
        var obj = {};
        obj.commands = "replace into companyimg (id,url) values("
            +" '"+  id  + " ',"
            +" '"+  url  + " '"

            + ")";
        db.runSqlInfo(obj,function(info,err){
            console.log("info is:",info);
            if(err){
                return cb({code:500,info:"修改数据库失败"})
            }else
                cb({code:200,info:'修改成功'});
        })
    }else {
        cb({code:200,info:'修改成功'});
    }


    //cb({code: 200,info:"上传成功"});
}





exports.uploadImage =  function(res,req,cb){
    var body = req.body;
    console.log("req.body",req.files);
    console.log("req.body",req);
    var filename = req.files.imgFile.originalFilename;
    var url = '/image/storyImage/' + new  Date().getTime() + ".png";
    var originalPath = req.files.imgFile.path;
    var targetPath = path.join(__dirname, '../public'+url);
    //path.dirname()
    fs.createReadStream(originalPath).pipe(fs.createWriteStream(targetPath));
     console.log("url is ...........","127.0.0.1:8080"+url)
    cb({ "error" : 0,code:200,message:'修改成功',url:'../'+ url});
}


//修改首页文字描述
exports.changeIndexMsg =  function(parame,req,cb){
    console.log("修改首页文字描述",parame);
    var msg = parame.msg;
    var obj = {};
    obj.commands = "update indexinfo set content = '" + msg +"' where id = 1 ";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err){
            return cb({code:500,info:"修改数据库失败"})
        }else
            cb({code:200,info:'修改成功'});
    })
}


//修改关于我们信息
exports.changeaboutusMsg =  function(parame,req,cb){
    console.log("修改关于我们信息",parame);
    var msg = parame.msg;
    var id = parame.id;
    var obj = {};
    obj.commands = "update aboutus set content = '" + msg +"' where id =  " + id;

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err){
            return cb({code:500,info:"修改数据库失败"})
        }else
            cb({code:200,info:'修改成功'});
    })
}




/**
 * 获取帐号信息
 * @param parame
 * @param req
 * @param cb
 */
exports.getUserInfo = function(parame,req,cb){
    console.log("获取帐号信息",parame);
    var obj = {};
    obj.commands = "select id,account,password,creator from plateuser;";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return ({code:500,info:[]});
        else
            cb({code:200,info:info});
    })
}

/**
 * 修改密码
 * @param parame
 * @param req
 * @param cb
 */
exports.updatePassword = function(parame,req,cb){
    console.log("获取帐号信息",parame);
    var obj = {};
    obj.commands = "update plateuser set password = '" + parame.password + "' where account = '" + parame.account + "'";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            cb({code:200,info:"修改成功"});
    })
}

/**
 * 删除账号
 * @param parame
 * @param req
 * @param cb
 */
exports.delUser = function(parame,req,cb){
    var obj = {};
    obj.commands = "delete from  plateuser  where account = '" + parame.account + "'";
    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            cb({code:200,info:"修改成功"});
    })
};


exports.userModify =function (parame,req,cb) {
    var id = parame.id;
    var obj = {};
    obj.commands = "select account,power from  plateuser  where id = '" + id + "'";

    db.runSqlInfo(obj,function(info,err){
        if(err)
            return cb({code:500,info:{}});
        else
            cb({code:200,info:info[0]});
    })

}


/**
 * 修改后台帐号
 * @param parame
 * @param req
 * @param cb
 */
exports.updateUser =function (parame,req,cb) {
    var form = parame;
    var power = form.power;//.toLowerCase();
    var userName = form.userName;//.toLowerCase();
    var obj = {};
    obj.commands = " update  plateuser set power = '" + power + "' where account = '" + userName + "'";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            var info = {code:500,msg:err}
            cb(info)
        }
        else {
            cb({code:200, msg:'操作成功!'})
        }
    });
}



/**
 * 添加后台帐号
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
exports.addBossUser =function (parame,req,cb) {
    var  account = parame.account;
    var  password = parame.password;

    if(!account || ! password){
        return cb({code:500,info:"缺少必要参数"});
    };

    var obj = {};
    obj.commands = "select count(*) total from plateuser where account = '" +  account + "';" ;
    ;

    db.runSqlInfo(obj,function(totalInfo,err){
        console.log("info is:",info);
        if(err){
            return cb({code:500,info:"获取信息错误"});
        }
        else{
            if(totalInfo[0].total >= 1){
                return  cb({code:500,info:'帐号已经存在'});
            }
            var obj = {};
            obj.commands = "insert into  plateuser (role,account,password,power,creator) values("
                + "'" + 3 + "',"
                + "'" + account + "',"
                + "'" + password + "',"
                + "'" + "{}" + "',"
                + "'" + req.session.user.username  + "');"
            ;

            db.runSqlInfo(obj,function(info,err){
                console.log("info is:",info);
                if(err){
                    return cb({code:500,info:"获取信息错误"});
                }
                else{
                    if(info.affectedRows >= 1)
                        return  cb({code:200,info:'添加成功'});
                    else
                        return  cb({code:500,info:'添加失败'});
                }
            })
        }
    })
}

